package chpn.test.rowset.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public    class ProxyHandler implements InvocationHandler {
	private Object concreteClass;
	Class<?> clazz ;

	public ProxyHandler(Object concreteClass) {
		this.concreteClass = concreteClass;
		clazz = concreteClass.getClass();
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
		try{
			method = clazz.getMethod(method.getName(), method.getParameterTypes() );
			Object object = method.invoke(concreteClass, args);// 普通的Java反射代码,通过反射执行某个类的某方法
			return object;
		}catch(NoSuchMethodException ex){
			if( method.getName().startsWith("set")){
				throw new NoSuchMethodException( ex.getMessage() +", 请使用setObject");
			}
			throw ex;
		}catch( InvocationTargetException ex){
			throw ex.getCause();
		}
	}

}